  //Declare global variables    
  var geocoder = null;
  var router = null;
  var routePoints = [];
  var routeID = null;
  
  function goMap24(from,to) {
	if (/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent)){
		document.getElementById('redbox').innerHTML = "Du ser ut til &aring; bruke Internet Explorer. Dessverre st&oslash;tter denne tjenesten enn&aring; ikke din nettleser. Pr&oslash;v om mulig Opera, Firefox, Safari eller en annen nettleser.<br /><br />Vi beklager bryderiet!";
	}
   Map24.loadApi( ["core_api", "wrapper_api"] , map24ApiLoaded );
  //  startRouting(from,to);
  }
  
  function map24ApiLoaded(){
  Map24.MapApplication.init( { NodeName: "maparea" } );
//	document.getElementById('js_hide').style='visibilty:visible';
/*	document.getElementById('js_hide').innerHTML ;*/
//Set loading picture
 //   document.getElementById('loading').innerHTML = " ";
  }
 
  function startRouting(start,destination){
   //Set loading picture
    document.getElementById('loading').innerHTML = '<img id="load_gif" src="view/images/loading.gif" alt="loading" />';
    //Retrieve start and destination of the route from the input fields
    var start = Map24.trim( $v('start') );
    var destination = Map24.trim( $v('destination') );
    
    //Check if the start and the destination form fields are empty
    if( start == "" ) { alert("Please enter start address!"); return; }
    if( destination == "" ) { alert("Please enter destination address!"); return; }
	  
    //Disable the button for starting a route calculation.
//    document.getElementById("button_calculate_route").disabled = true;
    
    //Create a geocoder stub
     geocoder = new Map24.GeocoderServiceStub();
//    var tmp = new Map24();
 //    geocoder = Map24.GeocoderServiceStub();
    
    //Geocode the start point of the route
    geocoder.geocode({ 
      SearchText: start, 
      //Define the name of the callback function that is called when the result is available on the client.
      CallbackFunction: setRouteEndPoint, 
      //Set a parameter that is passed to the callback function. The parameter defines that this is the start point.
      CallbackParameters: {position: "start"}
    });
    
    //Geocode the destination point of the route
    geocoder.geocode({
      SearchText: destination,  
      CallbackFunction: setRouteEndPoint,
      CallbackParameters: {position: "destination"}
    });
//	submitForm();
  }
  
  //Callback function that is called when the geocoding result is available.
  //The locations parameter contains an array with multiple alternative geocoding results.
  //The params parameter passes the value of CallbackParameters that specifies which route 
  //end point is returned (start or destination point).
  function setRouteEndPoint(locations, params){
 
    //Access the geocoded address and add it to the routePoints array.
    //The geocoded address is stored at the first position in the locations array.
    routePoints[ params.position ] = locations[0];
    
    //After both the start and the destination addresses are geocoded, this function calls the calculateRoute() function.
    if( typeof routePoints["start"] != "undefined" && typeof routePoints["destination"] != "undefined")
      calculateRoute(); 
  }
  
  //Calculate the route.
  function calculateRoute() {
    router = new Map24.RoutingServiceStub();
    router.calculateRoute({
      Start: routePoints["start"],
      Destination: routePoints["destination"],
      CallbackFunction: displayRoute,
      ShowRoute: false
    });
    document.getElementById("print").disabled = true;
    routePoints = [];
  }
  
  //Callback function used to access the calculated route of type Map24.WebServices.Route.
  //This function is called after the client has received the result from the routing service.
  function displayRoute( route ){
    
    //Remember the routeId. It is used e.g. to hide the route.
    routeID = route.RouteID;
    router.showRoute( {
      RouteId: routeID,
      Color: ['#00F', 150]
    });
    
    //Access the assumed time needed for traversing the route in hours
    var totalTime = ((route.TotalTime)/(60*60) ).toPrecision(3) 
    //Access the total lenght of the route in kilometers
    var totalLength = (route.TotalLength/1000) 
    //Create table with description of the route
    var div_content ="";
//	div_content += "Totalt " + totalTime + " timer<br>" ;     
    div_content += "<input type='hidden' name='total' value='"+ totalLength +"' />";
 //   div_content += "<br>";
//    div_content += "<input type='text' name='start' value='"+ /* fra by */ +"' /><br />";
    var byer = new Array();
    var aktuellstreng = new String();
    
	var kiloM = 0;
	kmByer = new Array();
	
//    Iterate through the route segments and output the step-by-step textual description of the route
    for(var i = 0; i < route.Segments.length; i++){
      for(var j = 0; j < route.Segments[i].Descriptions.length; j++){
      	  	 
         aktuellstreng = route.Segments[i].Descriptions[j].Text.replace(/(\[|\[\/)[0-9A-Z_]+\]/g, '' );
         list = new Array();
         list = aktuellstreng.split(" ");
		 
		 
		 
		 if(list[list.length-1] == "km.") {
			kiloM = kiloM + parseInt(list[list.length-2]);
		}
		else if(list[list.length-1] == "m.") {
			kiloM = kiloM + parseInt((list[list.length-2]/1000));
		}
		 
//		 div_content += "Km tot: " + kiloM + " <br>";
//		 div_content += "La til " + list[list.length-2] + " <br><br>";
		 
         for (k=0;k<list.length;k++) {
        	if(list[k] == "entering") {
        		
        		list2 = new Array();
         		list2 = aktuellstreng.split("entering");
        		kmByer.push(kiloM);
        		byer.push(list2[1].substring(0,list2[1].length-1));
        	}
		 }	
       }
    }
    for(i=0;i<byer.length;i++) {
    	div_content += "<input type='hidden' name='by" + i +"' value='"+byer[i]+"' >";
		div_content += "<input type='hidden' name='km" + i +"' value='"+kmByer[i]+"' >"; //<br />";
    }
//	div_content += "</br>";
    document.getElementById('getWeather').disabled = false;
    document.getElementById('routeDescription').innerHTML = div_content;
    document.getElementById('loading').innerHTML = "";
	document.getElementById('redbox').innerHTML = "Klikk 'vis v&aelig;rmelding' for &aring; g&aring; videre";
//	document.getElementById('step2').submit();
    //document.getElementById('span').innerHTML = "";
  }
  
  //Show route
  function showRoute(routeID) {
    router.showRoute({RouteId: routeID});
    document.getElementById("button_show_route").disabled = true;
    document.getElementById("button_hide_route").disabled = false;
    document.getElementById("button_remove_route").disabled = false; 
  }
  
  //Hide route
  function hideRoute(routeID) {
    router.hideRoute({RouteId: routeID});
    document.getElementById("button_show_route").disabled = false;
    document.getElementById("button_hide_route").disabled = true;
    document.getElementById("button_remove_route").disabled = true;
  }
  
  //Remove route
  function removeRoute(routeID) {
    router.removeRoute({RouteId: routeID});
    //Delete route description  
    document.getElementById("routeDescription").innerHTML = "";
    document.getElementById("button_calculate_route").disabled = false;
    document.getElementById("button_show_route").disabled = true;
    document.getElementById("button_hide_route").disabled = true;
    document.getElementById("button_remove_route").disabled = true;
    document.getElementById("print").disabled = true; 
  }
  
  //Print Description function. 
  //The function opens a print preview window of the route description and one can choose the printer. 
  function printRouteDescription(){
    var printContent = document.getElementById("routeDescription");
    var windowPrint = window.open('','','left=0,top=0,width=0,height=0,toolbar=0,scrollbars=0,status=0');
    windowPrint.document.write(printContent.innerHTML);
    windowPrint.document.close();
    windowPrint.focus();
    windowPrint.print();
    windowPrint.close();
  }
    
  //Helper function for accessing the div specified in the id parameter.
  //The function checks first if the div contains content.
  function $v( id ) { 
    return   (document.getElementById( id ).value != "undefined") ?  
    document.getElementById( id ).value : ""; 
  }
